﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Services.Models;
using Services.Models.XmlSoccer;
using Match = Services.Models.XmlSoccer.Match;

namespace Services.Mappers {

    public static class Mapper {

        public static MatchModel MapToMatchModel(this Match match) {
            var model = new MatchModel {
                Id = match.Id,
                Time = match.Time.StartsWith("-") ? "90'" : match.Time,
                League = match.League,
                AwayFormation = match.AwayTeamFormation,
                AwayGoals = match.AwayGoals,
                AwayLineupsGoalkeeper = match.AwayLineupGoalkeeper.Trim(),
                AwayLineupsDefence = string.IsNullOrEmpty(match.AwayLineupDefense) ? new List<string>() : match.AwayLineupDefense.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()).ToList(),
                AwayLineupsForward = string.IsNullOrEmpty(match.AwayLineupForward) ? new List<string>() : match.AwayLineupForward.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()).ToList(),
                AwayLineupsMidfield = string.IsNullOrEmpty(match.AwayLineupMidfield) ? new List<string>() : match.AwayLineupMidfield.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()).ToList(),
                AwayOdds = match.WilliamHill_Away,
                AwayRedCards = match.AwayTeamRedCardDetails.MapToEventModel(),
                AwayScorers = match.AwayGoalDetails.MapToEventModel(),
                AwaySubsIn = new List<EventModel>(),
                AwaySubsOut = new List<EventModel>(),
                AwayTeamId = match.AwayTeam_Id,
                AwayTeam = string.IsNullOrEmpty(match.AwayTeam) ? match.Awayteam : match.AwayTeam,
                AwayYellowCards = match.AwayTeamYellowCardDetails.MapToEventModel(),
                Date = match.Date.ToShortDateString(),
                DrawOdds = match.WilliamHill_Draw,
                HomeFormation = match.HomeTeamFormation,
                HomeGoals = match.HomeGoals,
                HomeLineupsGoalkeeper = match.HomeLineupGoalkeeper.Trim(),
                HomeLineupsDefence = string.IsNullOrEmpty(match.HomeLineupDefense) ? new List<string>() : match.HomeLineupDefense.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()).ToList(),
                HomeLineupsForward = string.IsNullOrEmpty(match.HomeLineupForward) ? new List<string>() : match.HomeLineupForward.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()).ToList(),
                HomeLineupsMidfield = string.IsNullOrEmpty(match.HomeLineupMidfield) ? new List<string>() : match.HomeLineupMidfield.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()).ToList(),
                HomeOdds = match.WilliamHill_Home,
                HomeRedCards = match.HomeTeamRedCardDetails.MapToEventModel(),
                HomeScorers = match.HomeGoalDetails.MapToEventModel(),
                HomeSubsIn = new List<EventModel>(),
                HomeSubsOut = new List<EventModel>(),
                HomeTeamId = match.HomeTeam_Id,
                HomeTeam = string.IsNullOrEmpty(match.HomeTeam) ? match.Hometeam : match.HomeTeam,
                HomeYellowCards = match.HomeTeamYellowCardDetails.MapToEventModel(),
                Round = match.Round,
                Stadium = match.Location
            };

            var regex = new Regex("^(?<time>.*?): (?<direction>.*?) (?<player>.*?)$");

            var entries = match.HomeSubDetails.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            foreach(var entry in entries) {
                var m = regex.Match(entry);
                if(m.Success) {
                    var time = m.Groups["time"].Value;
                    var player = m.Groups["player"].Value;
                    var direction = m.Groups["direction"].Value;
                    if(direction == "in") {
                        model.HomeSubsIn.Add(new EventModel { Time = time, Player = player });
                    } else {
                        model.HomeSubsOut.Add(new EventModel { Time = time, Player = player });
                    }
                }
            }

            entries = match.AwaySubDetails.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            foreach(var entry in entries) {
                var m = regex.Match(entry);
                if(m.Success) {
                    var time = m.Groups["time"].Value;
                    var player = m.Groups["player"].Value;
                    var direction = m.Groups["direction"].Value;
                    if(direction == "in") {
                        model.AwaySubsIn.Add(new EventModel { Time = time, Player = player });
                    } else {
                        model.AwaySubsOut.Add(new EventModel { Time = time, Player = player });
                    }
                }
            }


            return model;
        }

        public static List<EventModel> MapToEventModel(this string value) {
            var list = new List<EventModel>();
            if(!string.IsNullOrEmpty(value)) {
                var reds = value.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                foreach(var red in reds) {
                    var temp = red.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
                    if(temp.Length == 2) {
                        list.Add(new EventModel { Time = temp[0].Trim(), Player = temp[1].Trim() });
                    }
                }
            }
            return list;
        }



        public static MatchShortModel MapToMatchShortModel(this Match match) {
            var model = new MatchShortModel {
                Id = match.Id,
                Time = match.Time.StartsWith("-") ? "90'" : match.Time,
                AwayGoals = match.AwayGoals,
                AwayTeamId = match.AwayTeam_Id,
                AwayTeam = string.IsNullOrEmpty(match.AwayTeam) ? match.Awayteam : match.AwayTeam,
                HomeGoals = match.HomeGoals,
                HomeTeamId = match.HomeTeam_Id,
                HomeTeam = string.IsNullOrEmpty(match.HomeTeam) ? match.Hometeam : match.HomeTeam,
            };

            return model;
        }

        public static IList<LeagueWithMatchesModel> MapToLeagueModel(this XmlSoccerMatchList matches) {
            var leagues = new List<LeagueWithMatchesModel>();
            foreach(var match in matches) {
                var league = leagues.FirstOrDefault(x => x.League == match.League);
                if(league == null) {
                    league = new LeagueWithMatchesModel {
                        League = match.League,
                        Matches = new List<MatchShortModel>()
                    };

                    leagues.Add(league);
                }
                league.Matches.Add(match.MapToMatchShortModel());
            }

            return leagues;
        }
    }
}
